/**
 * Copyright (c) 2009-2011, chunquedong(YangJiandong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 
 * History:
 *     2010-05-05  Jed Young  Creation
 */
package chunmap.service.wms;

import java.io.PrintStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import chunmap.model.elem.Envelope;
import chunmap.service.Service;
import chunmap.util.Logger;

/**
 * @author yangjiandong
 * 
 */
public class WebMapService implements Service {
	private static final Logger Log = new Logger(
			Logger.Debug,WebMapService.class.getName());
	private Capabilities capabilities;
	private GetMap getMap;
	private static final String defaultFormat = "png";

	public WebMapService(Capabilities capabilities) {
		this.capabilities = capabilities;
		getMap = new GetMap(capabilities);
	}

	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, java.io.IOException {

		// String version=request.getParameter("VERSION");
		// String updateSequence=request.getParameter("UPDATESEQUENCE");
		String requestStr = request.getParameter("REQUEST");

		PrintStream out = new PrintStream(response.getOutputStream());
		if (requestStr.equalsIgnoreCase("GetCapabilities")) {
			String s=request.getRequestURL().toString();
			int i=s.indexOf("?");
			if(i!=-1)s=s.substring(0,i);
			capabilities.setUrl(s);
			capabilities.setLink(s);
			
			//Log.log(Logger.Debug, "url is " + s);
			capabilities.reponse(out);
		} else {
			String bbox = request.getParameter("BBOX");
			Envelope envelop = parseEnvelop(bbox);
			int width = Integer.valueOf(request.getParameter("WIDTH"));
			int height = Integer.valueOf(request.getParameter("HEIGHT"));
			String format = parseFormat(request.getParameter("FORMAT"));
			
			if (requestStr.equalsIgnoreCase("GetMap")) {
				getMap.setEnvelop(envelop);
				getMap.setCapbilities(capabilities);
				getMap.setHeight(height);
				getMap.setWidth(width);
				getMap.setFormatName(format);
	
				getMap.reponse(out);
			}else if (requestStr.equalsIgnoreCase("GetFeatureInfo")) {
				GetFeature getInfo=new GetFeature();
				getInfo.capbilities=capabilities;
				getInfo.envelop=envelop;
				getInfo.formatName=format;
				getInfo.height=height;
				getInfo.width=width;
				
				getInfo.service(request, response);
			} 
			else {
				Log.log(Logger.Debug, "不支持的请求" + requestStr);
			}
		}
	}

	private Envelope parseEnvelop(String bbox) {
		String[] ss = bbox.split(",");
		double minx = Double.valueOf(ss[0]);
		double miny = Double.valueOf(ss[1]);
		double maxx = Double.valueOf(ss[2]);
		double maxy = Double.valueOf(ss[3]);

		Envelope envelop = new Envelope(minx, miny, maxx, maxy);
		return envelop;
	}

	private String parseFormat(String format) {
		if (format != null) {
			String[] ss = format.split("/");
			if (ss.length == 2)
				return ss[1];
		}
		return defaultFormat;
	}
}